<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;

use app\common\model\Config as ConfigModel;

/**
 * 配置管理
 * @package app\admin\controller
 */
class Config extends AdminBase
{
    private $model;

    public function _initialize()
    {
        $this->model=new ConfigModel;
    }
    # 站点信息修改
    public function conf()
    {
        if (!request()->isPost()) {
            $data=$this->model->getAllData();
            $this->assign('data', $data);
            return $this->fetch();
        }
        $data=input('post.');
        if (!$this->model->updateData($data)) {
            $this->error('配置修改时发生错误');
        }
        $this->success('配置修改成功');
    }
    # 关于站点页面
    public function about()
    {
        if (!request()->isPost()) {
            $data=$this->model->getByName('site_about');
            $this->assign('data', $data->value);
            return $this->fetch();
        }
        $content=input('post.content', '');
        if (!$this->model->updateAbout($content)) {
            $this->error('关于页面编辑时发生错误');
        }
        $this->success('关于页面编辑成功');
    }
    # 上传图片
    public function upload()
    {
        $file = request()->file('image');
        $info = $file->validate(['size'=>1024*1024*2,'ext'=>'jpg,jpeg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
        if (!$info) {
            return json(['errno'=>1,'msg'=>$info->getError()]);
        }
        $url= DS .'uploads'. DS .$info->getSaveName();
        $url=str_replace("\\", "/", $url);
        $shortUrl=$info->getSaveName();
        return json(['errno'=>0,'msg'=>'上传成功','url'=>$url,'short'=>$shortUrl]);
    }
}
